<template>
    <div class="layout-route">
        <div class="card card-search">
            <el-form :model="queryParams" ref="queryForm" v-show="showSearch" inline label-width="100px">
                <el-form-item label="店铺" prop="shop_id">
                    <el-select v-model="queryParams.shop_id" placeholder="请选择店铺" clearable size="small" style="width: 240px">
                        <el-option v-for="(dict, index) in shopList" :key="index" :label="dict.shop_name" :value="dict.shop_id" />
                    </el-select>
                </el-form-item>
                <el-form-item label="创建时间" prop="create_time">
                    <el-date-picker v-model="queryParams.create_time" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" style="width: 240px"> </el-date-picker>
                </el-form-item>
                <el-form-item label=" ">
                    <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
        </div>

        <div class="card">
            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button type="warning" plain icon="el-icon-download" :loading="download" :disabled="download" @click="handleExport">导出</el-button>
                </el-col>
                <right-toolbar :showSearch.sync="showSearch" :columns="columns" @queryTable="getList"></right-toolbar>
            </el-row>
            <el-table ref="sortTable" v-loading="loading" :data="tableData" row-key="id" border script @sort-change="handleSortChange">
                <el-table-column label="店铺" prop="shop_id" min-width="100" show-overflow-tooltip>
                    <template slot-scope="scope">
                        {{ scope.row.shop_id | formatLabelByValueFuzzy(shopList, {label:'shop_name', value: 'shop_id'}) }}
                </template>
                </el-table-column>
                <el-table-column label="页面类型" prop="page_type" min-width="100" show-overflow-tooltip v-if="showColumn('page_type')"></el-table-column>
                <el-table-column label="页面路径" prop="page_path" min-width="200" show-overflow-tooltip v-if="showColumn('page_path')"></el-table-column>
                <el-table-column label="商店访客" prop="total_visitors" width="100" sortable="custom" :sort-orders="['descending', 'ascending']" v-if="showColumn('total_visitors')"></el-table-column>
                <el-table-column label="访问量" prop="total_sessions" width="100" sortable="custom" :sort-orders="['descending', 'ascending']" v-if="showColumn('total_sessions')"></el-table-column>
                <el-table-column label="加购访问" prop="total_carts" width="100" sortable="custom" :sort-orders="['descending', 'ascending']" v-if="showColumn('total_carts')"></el-table-column>
                <el-table-column label="结账访问" prop="total_checkouts" width="100" sortable="custom" :sort-orders="['descending', 'ascending']" v-if="showColumn('total_checkouts')"></el-table-column>
                <el-table-column label="创建时间" prop="create_time" min-width="100" v-if="showColumn('create_time')"></el-table-column>
            </el-table>
            <pagination v-show="total > 0" :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="getList" />
        </div>
    </div>
</template>
<script>
import { getReportVisitPathList, exportReportVisitPathList } from '@/api/report'
import { getShopList } from '@/api/shop'
import { exportFile } from '@/utils/index'
import { parseTime } from '@/utils/quickadmin'
export default {
    name: 'ReportVisitinfo',
    components: { },
    data() {
        return {
            // 遮罩层
            loading: true,
            download: false,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 表格数据
            tableData: [],
            shopList: [],
            // data中定义Arr数组，用来存放筛选列
            orderArray: [],
            sorts: [], // 用于存储多列排序信息的数组

            pageInfo: {
                page: 1,
                limit: 10,
            },
            // 查询参数
            queryParams: {
                seller_sku: undefined,
                spu: undefined,
                create_time: [],
            },
            columns: [
                {label:'页面类型', prop: 'page_type', visible: true },
                {label:'页面路径', prop: 'page_path', visible: true },
                {label:'访客', prop: 'total_visitors', visible: true },
                {label:'访问', prop: 'total_sessions', visible: true },
                {label:'添加到购物车', prop: 'total_carts', visible: true },
                {label:'进入结算流程', prop: 'total_checkouts', visible: true },
                {label:'访问持续时间', prop: 'avg_duration', visible: true },
                {label:'跳出率', prop: 'total_bounce_rate', visible: true },
                {label:'转化率', prop: 'total_conversion', visible: true },
                {label:'成功转换的访问次数', prop: 'total_orders_placed', visible: true },
                {label:'页面浏览量', prop: 'total_pageviews', visible: true },
                // {label:'运营', prop: 'admin_name', visible: true },
                {label:'创建时间', prop: 'create_time', visible: true },
            ]
        }
    },
    async created() {
      this.loading = true
      try {
          const result = await getShopList()
          this.shopList = result?.data?.list ?? []
      } catch (error) {
          this.loading = false
          console.log(error)
      }
      this.getList()
  },
    methods: {
        showColumn(prop) {
            const data = this.columns.find(c => c.prop === prop)
            return data?.visible ?? true
        },
        resetSort() {
            this.queryParams.total_visitors = undefined
            this.queryParams.total_sessions = undefined
            this.queryParams.total_carts = undefined
            this.queryParams.total_checkouts = undefined
            this.queryParams.avg_duration = undefined
            this.queryParams.total_bounce_rate = undefined
            this.queryParams.total_conversion = undefined
            this.queryParams.total_orders_placed = undefined
            this.queryParams.total_pageviews = undefined
        },
        handleSortChange({ column, prop, order }) {
            this.resetSort()
            this.queryParams[prop] = order === 'descending' ? 'desc' : order === 'ascending' ? 'asc' : undefined
            this.getList()
        },
        showProductStockDialog(row) {
            this.stockDialog.visible = true
            this.stockDialog.data = row
        },
        showDetailDialog(row) {
            this.detailDialog.visible = true
            this.detailDialog.data = row
        },
        handleExport() {
            this.download = true
            const qyparams = {
                ...this.pageInfo,
                ...this.queryParams,
            }
            if (qyparams.create_time?.length == 2) {
                qyparams.create_time = [parseTime(qyparams.create_time[0], 'y-m-d'), parseTime(qyparams.create_time[1], 'y-m-d')]
            }
            exportReportVisitPathList(qyparams)
                .then(res => {
                    exportFile(res, () => {
                        this.download = false
                    })
                })
                .catch(err => {
                    console.log(err)
                    this.download = false
                })
        },
        handleClick() {
            this.getList()
        },

        //列表
        getList() {
            this.loading = true
            const qyparams = {
                ...this.pageInfo,
                ...this.queryParams,
            }
            if (qyparams.create_time?.length == 2) {
                qyparams.create_time = [parseTime(qyparams.create_time[0], 'y-m-d'), parseTime(qyparams.create_time[1], 'y-m-d')]
            }
            getReportVisitPathList(qyparams)
                .then(response => {
                    this.tableData = response.data.list
                    this.tableData.map(item => {
                        item.product_images = item.product_images?.replaceAll('\\', '')
                        return { ...item }
                    })
                    this.total = response.data.total
                })
                .finally(() => {
                    this.loading = false
                })
        },

        // 搜索按钮操作
        handleQuery() {
            this.pageInfo.page = 1
            this.getList()
        },

        // 重置按钮操作
        resetQuery() {
            this.resetForm('queryForm')
            this.resetSort()
            this.$refs.sortTable.clearSort()
            this.handleQuery()
        },

         //更新排序
        updateSort() {
            this.$confirm(`预计等待一分钟`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            })
                .then(() => {
                    this.baseRequest_updateSort({ })
                })
                .catch(err => {})
        },
        baseRequest_updateSort({ }) {
            this.request
                .post("/product/ProductInventory/updateSort", { })
                .then(response => {
                    console.log('response',response);
                    
                    if(response.code == 200){
                        this.$message.success('操作成功');
                    }
                    console.log('test',13);
                    
                    this.getList()
                })
                .finally(() => {
                })
        },
    },
}
</script>
<style lang="scss" scoped>
.total-warp {
    font-size: 18px;
    font-weight: bold;
    padding: 8px 0 16px 0;
}
    /* 统计卡片 */
    .chart-card {
        color: #606266;
        margin: 10px 0;
    }
    .chart-card-num {
        font-size: 30px;
    }

    .chart-card-content {
        height: 30px;
        .content-title {
            font-weight: bold;
            font-size: 20px;
            color: #303133;
        }
    }
</style>
